//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}

/**
 * 连小龙
 * 直接发送ajax,渲染页面
 */
let pagenum=1;
let totalrows;
let totalPage;
let n=1;
$(document).ready(function (){
    queryAllFirms();

});

function queryAllFirms(){
    $.ajax({
        url:"/manufacturer/queryAllFirm",
        type:"post",
        data:{pagenum:n},
        dataType:"json",
        success:function (r){
            totalPage = r.totalPage;
            var str = "<tr>" +
                "          <th>序号</th>" +
                "          <th>厂商名称</th>" +
                "          <th>负责人</th>" +
                "          <th>联系电话</th>" +
                "          <th>所属区域</th>" +
                "          <th>状态</th>" +
                "          <th>创建时间</th>" +
                "          <th>创建人</th>" +
                "          <th>操作</th>" +
                "        </tr>";
            for (let i = 0; i < r.data.length; i++) {
                var status = r.data[i].status;
                if (status=="0") {
                    str += "<tr>" +
                        "<td>" + r.data[i].firmId + "</td>" +
                        "<td>" + r.data[i].firmName + "</td>" +
                        "<td>" + r.data[i].firmFounder + "</td>" +
                        "<td>" + r.data[i].firmTel + "</td>" +
                        "<td>" + r.data[i].province.pName + r.data[i].city.c_Name + "</td>" +
                        "<td>不可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].users.uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='queryFirmDetails(this)'>查看详情</a>" +
                        "<a href='manufacturerUpdate.html' class='tablelink' onclick='updateFirmDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\">注销</a>" +
                        "</td></tr>";
                }else{
                    str += "<tr>" +
                        "<td>" + r.data[i].firmId + "</td>" +
                        "<td>" + r.data[i].firmName + "</td>" +
                        "<td>" + r.data[i].firmFounder + "</td>" +
                        "<td>" + r.data[i].firmTel + "</td>" +
                        "<td>" + r.data[i].province.pName + r.data[i].city.c_Name + "</td>" +
                        "<td>可用</td><td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].users.uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='queryFirmDetails(this)'>查看详情</a>" +
                        "<a href='manufacturerUpdate.html' class='tablelink' onclick='updateFirmDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\">注销</a>" +
                        "</td></tr>";
                }
            }
            $(".tablelist").html(str);
            $("#totalRows").html(r.totalRows);
            totalrows = $("#totalRows").html();
            $("#pagenum").html(r.pageNum);
            pagenum = $("#pagenum").html();

        }
    })
}

function shangyiye(){

    if (Number(pagenum)<=1){
        alert("已经是第一页了");
    }else{
        n = pagenum-1;
        queryAllFirms();
    }
}

function xiayiye(){
    if (Number(pagenum)>=Number(totalPage)){
        alert("已经是最后一页了");
    }else{
        n = Number(pagenum)+1;
        queryAllFirms();
    }
}

/**
 * 查询单个数据通过多个条件模糊查询
 */
function queryByCondition(){
    var sta = $(".select_status option:selected").text();
    var provin = $(".select_province option:selected").text();
    var cit = $(".select_city option:selected").text();
    var fname = $("#fname").val();
    var fconpany = $("#fconpany").val();
    var statu;
    if (sta=="可用"){
        statu = 1;
    }else if(sta=="不可用"){
        statu = 0;
    }else if (sta=="请选择"){
        statu = "";
    }

    $.ajax({
        url:"/manufacturer/queryOneFirm",
        type:"post",
        data:{pagenum:n,fconpany:fconpany,fname:fname,provin:provin,cit:cit,statu:statu},
        dataType:"json",
        success:function (r){
            totalPage = r.totalPage;
            var str = "<tr>" +
                "          <th>序号</th>" +
                "          <th>厂商名称</th>" +
                "          <th>负责人</th>" +
                "          <th>联系电话</th>" +
                "          <th>所属区域</th>" +
                "          <th>状态</th>" +
                "          <th>创建时间</th>" +
                "          <th>创建人</th>" +
                "          <th>操作</th>" +
                "        </tr>";
            for (let i = 0; i < r.data.length; i++) {
                var status = r.data[i].status;
                if (status=="0") {
                    str += "<tr>" +
                        "<td>" + r.data[i].firmId + "</td>" +
                        "<td>" + r.data[i].firmName + "</td>" +
                        "<td>" + r.data[i].firmFounder + "</td>" +
                        "<td>" + r.data[i].firmTel + "</td>" +
                        "<td>" + r.data[i].province.pName + r.data[i].city.c_Name + "</td>" +
                        "<td>不可用</td>" +
                        "<td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].users.uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='queryFirmDetails(this)'>查看详情</a>" +
                        "<a href='manufacturerUpdate.html' class='tablelink' onclick='updateFirmDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\">注销</a>" +
                        "</td></tr>";
                }else{
                    str += "<tr>" +
                        "<td>" + r.data[i].firmId + "</td>" +
                        "<td>" + r.data[i].firmName + "</td>" +
                        "<td>" + r.data[i].firmFounder + "</td>" +
                        "<td>" + r.data[i].firmTel + "</td>" +
                        "<td>" + r.data[i].province.pName + r.data[i].city.c_Name + "</td>" +
                        "<td>可用</td><td>" + getFormtTime(r.data[i].createTime,false) + "</td>" +
                        "<td>" + r.data[i].users.uname + "</td>" +
                        "<td><a href='javascript:void(0);' class='tablelink' onclick='queryFirmDetails(this)'>查看详情</a>" +
                        "<a href='manufacturerUpdate.html' class='tablelink' onclick='updateFirmDetails(this)'>修改</a>" +
                        "<a href='javascript:void(0);' class='tablelink' onclick=\"tipOpen('是否确认注销此条信息？')\">注销</a>" +
                        "</td></tr>";
                }
            }
            $(".tablelist").html(str);
            $("#totalRows").html(r.totalRows);
            totalrows = $("#totalRows").html();
            $("#pagenum").html(r.pageNum);
            pagenum = $("#pagenum").html();

        }
    })
}



/**
 * 查询单个数据详情
 */

function queryFirmDetails(e){
    var id = $(e).parents("tr")[0].children[0].innerHTML;;


    var proCity = $(e).parents("tr")[0].children[4].innerHTML;
    var status = $(e).parents("tr")[0].children[5].innerHTML;
    var creatTime = $(e).parents("tr")[0].children[6].innerHTML;
    var creatPeople = $(e).parents("tr")[0].children[7].innerHTML;

    var ck = $(e).html();

    location.replace("manufacturerView.html");
//设置sessionStorage：-----------主要代码-------------------------------
    window.sessionStorage.setItem('id', id);
    window.sessionStorage.setItem('proCity', proCity);
    window.sessionStorage.setItem('status', status);
    window.sessionStorage.setItem('creatTime', creatTime);
    window.sessionStorage.setItem('creatPeople', creatPeople);
    window.sessionStorage.setItem('ck', ck);

}

function updateFirmDetails(e){
    var id = $(e).parents("tr")[0].children[0].innerHTML;;

    var proCity = $(e).parents("tr")[0].children[4].innerHTML;
    var status = $(e).parents("tr")[0].children[5].innerHTML;
    var creatTime = $(e).parents("tr")[0].children[6].innerHTML;
    var creatPeople = $(e).parents("tr")[0].children[7].innerHTML;

    var ck = $(e).html();

    location.replace("manufacturerUpdate.html");
//设置sessionStorage：-----------主要代码-------------------------------
    window.sessionStorage.setItem('id', id);
    window.sessionStorage.setItem('proCity', proCity);
    window.sessionStorage.setItem('status', status);
    window.sessionStorage.setItem('creatTime', creatTime);
    window.sessionStorage.setItem('creatPeople', creatPeople);
    window.sessionStorage.setItem('ck', ck);

}

